package gzj.controller;


import gzj.pojo.*;
import gzj.service.moment.MomentService;
import com.alibaba.fastjson.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @Author ☞☞ 祝晨轩
 * @DateTime ☞☞ 2019/7/13 8:21
 * 不积跬步，无以至千里；不积小流，无以成江海。
 * 键盘敲烂，月薪过万。
 */
@Controller
@RequestMapping("/moment")
public class MomentController {
    @Resource
    private MomentService momentService;

//跳转到时刻的预加载
    @RequestMapping("/list.html")
    public String getUserList(@RequestParam(value = "school",required = false)String school_id,
                              @RequestParam(value = "category",required = false)String category_id,
                              @RequestParam(value = "pageIndex",required = false)String pageIndex,
                              HttpServletRequest request,HttpSession session){
        PageSupport pageSupport=new PageSupport();
        pageSupport.setPageSize(5);
        if(category_id==null){
            category_id="0";
        }
        if(pageIndex==null){
            pageIndex="1";
        }
        pageSupport.setCurrentPageNo(Integer.parseInt(pageIndex));
        if(school_id==null){
            school_id="0";
        }
        pageSupport.setTotalCount(this.momentService.getCountBySchoolAndCategory(Integer.parseInt(school_id),Integer.parseInt(category_id)));

        List<Moment> momentList=null;
        if("0".equals(school_id) && !"0".equals(category_id)){
            momentList=this.momentService.getMomentListByCategory(Integer.parseInt(category_id),pageSupport);
        }else if("0".equals(category_id) && !"0".equals(school_id)){
            momentList=this.momentService.getMomentListBySchool(Integer.parseInt(school_id),pageSupport);
        }else if("0".equals(school_id) && "0".equals(category_id)){
            momentList=this.momentService.selectMomentList(pageSupport);
        }else{
            momentList=this.momentService.getMomentList(Integer.parseInt(category_id),Integer.parseInt(school_id),pageSupport);
        }

        for(Moment moment:momentList){
            String showContent;
            if(moment.getContent().length()>10){
                showContent=moment.getContent().substring(0,11)+"...";
                moment.setContent(showContent);
                showContent=null;
            }
            String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(moment.getCreateDate());
            moment.setShowDate(date);
            String category = this.momentService.getCategoryByCategory_id(moment.getCategory_id());
            moment.setCategory(category);
            String school=this.momentService.getSchoolBySchool_id(moment.getSchool_id());
            moment.setSchool(school);
            System.out.println(moment.getAuthor());
        }
        List<School> schoolList = this.momentService.getSchoolList();  //得到学校列表
        List<Category> categoryList=this.momentService.getCategoryList();//得到分类列表
        request.setAttribute("momentList",momentList);
        request.setAttribute("schoolList",schoolList);
        request.setAttribute("categoryList",categoryList);
        request.setAttribute("totalPageCount",pageSupport.getTotalPageCount());
        request.setAttribute("totalCount",pageSupport.getTotalCount());
        request.setAttribute("currentPageNo",pageSupport.getCurrentPageNo());
        request.setAttribute("backSchool",school_id);
        request.setAttribute("backCategory",category_id);
        return "moment";
    }
//跳转到时刻页面
    @RequestMapping("/addMoment.html")
    public String addMoment(HttpSession session,HttpServletRequest request){

        List<Category> categoryList=this.momentService.getCategoryList();//得到分类列表
        request.setAttribute("categoryList",categoryList);
        return "addmoment";
    }
//保存添加的时刻
    @RequestMapping("/addMomentSave.html")
    public String addMomentSave(@RequestParam(value = "category_id",required = false)String category_id,
                                @RequestParam(value = "content",required = false)String content,
                                @RequestParam(value = "a_idPicPath") MultipartFile pictureFile,
                                HttpServletResponse response){
        //上传文件
        //设置图片名称,使用UUID
        String picName= UUID.randomUUID().toString();
        //获取文件名
        String oriName=pictureFile.getOriginalFilename();
        //获取图片后缀
        String extName=oriName.substring(oriName.lastIndexOf("."));
        //上传文件
        try {
            pictureFile.transferTo(new File("D:\\ideaIU-2018.3.5.win\\Project\\gzj_1.0\\web\\statics\\images\\"+picName+extName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String img=picName+extName;//上传到数据库的文件名
        int rs=this.momentService.addMoment(Integer.parseInt(category_id),Integer.parseInt("1"),"测试",content,img);
        if(rs>0){
            return "redirect:/moment/list.html";
        }
        return "addmoment";
    }
//删除指定时刻
    @RequestMapping("/del.html")
    public String del(@RequestParam(value = "moment_id")String moment_id){
        int rs=this.momentService.del(Integer.parseInt(moment_id));
        if(rs>0) {
            return "redirect:/moment/list.html";
        }
        return "error";
    }
//跳转到评论页面
    @RequestMapping("/Comment.html")
    public String addComment(@RequestParam(value = "moment_id")String moment_id,HttpServletRequest request){
        Moment moment=this.momentService.getMoment(Integer.parseInt(moment_id));
        String momentdate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(moment.getCreateDate());
        moment.setShowDate(momentdate);
        String img=moment.getImg();
        //拿文件后缀
        if(img!=null && !"".equals(img)) {
            int begin = moment.getImg().indexOf(".");

            int last = moment.getImg().length();
            String px = moment.getImg().substring(begin,last);
            //判断文件后缀
            if (".png".equals(px) || ".jpg".equals(px) || ".gif".equals(px)) {
                request.setAttribute("isImg", 1);
            } else if (".avi".equals(px) || ".mp4".equals(px)) {
                request.setAttribute("isImg", 0);
            }
        }
        List<MomentComment> list=this.momentService.seletComment(Integer.parseInt(moment_id));
        for(MomentComment comment:list){
            String date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(comment.getCreateTime());
            comment.setShowDate(date);

        }
        request.setAttribute("moment",moment);
        request.setAttribute("MomentCommentList",list);
        return "addcomment";
    }
//保存评论
    @RequestMapping("/addCommentSave.html")
    public String addCommentSave(@RequestParam(value = "moment_id")String moment_id,
                                 @RequestParam(value = "content")String content){
        int rs=this.momentService.addCommen(Integer.parseInt(moment_id),content,"评论人");
        if(rs>0) {
            return "redirect:/moment/Comment.html?moment_id="+moment_id;
        }
        return "error";
    }
//点赞
    @RequestMapping("/like.html")
    @ResponseBody
    public Object addLike(@RequestParam(value = "moment_id",required = false)String moment_id){
        int likeResult = this.momentService.addLike(Integer.parseInt(moment_id));
        HashMap<String,String> map=new HashMap<>();
        if(likeResult>0){
            map.put("likeResult","true");
        }else{
            map.put("likeResult","false");
        }
        return JSONArray.toJSONString(map);
    }
//根据uid查询时刻
    @RequestMapping("/getMomentByUid.html")
    public String getMomentByUid(@RequestParam(value = "uid")String uid, HttpServletRequest request){
        List<Moment> momentList=this.momentService.getMomentByUid(Integer.parseInt(uid));
        request.setAttribute("momentList",momentList);
        return "moment";
    }


}
